package wt.hpad.activity.cart;

import android.app.Activity;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import wt.hpad.ConstantFields;
import wt.hpad.dialog.SimpleDialog;
import wt.hpad.dialog.SimpleDialogHandler;
import wt.hpad.dialog.TimeLoadingDialog;
import wt.hpad.model.remote.HttpRequest;
import wt.hpad.utils.DialogUtil;
import wt.hpad.utils.GsonUtil;

import static wt.hpad.ConstantFields.CART_CURRENT;
import static wt.hpad.ConstantFields.CURRENT_TABLE;

/**
 * Created by wt on 1.3.
 */

public class CartPresenter implements CartContract.Presenter {
    CartContract.View mCartFrag;
    private TimeLoadingDialog mLoadingDialog;

    public CartPresenter(CartContract.View view) {
        mCartFrag = view;
        mCartFrag.setPresenter(this);
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {

    }

    @Override
    public void placeOrder(final Activity activity) {
        if (CART_CURRENT.size() == 0) {
            DialogUtil.showToastOnUI("没有点菜");
            return;
        }
        SimpleDialog confiemDlg = new SimpleDialog(activity);
        confiemDlg.setContent("核对完成并下单?");
        confiemDlg.show();
        confiemDlg.setHandler(new SimpleDialogHandler() {
            @Override
            public void onConfirm() {
                // 购物车中已点菜 to json
                String dishes = GsonUtil.toJsonString(CART_CURRENT);
                final HttpRequest httpRequest = new HttpRequest();
                final Observer<? super Integer> placeObsr = placeObsr();
                // 暂时不给 "服务员" 和 "就餐人数" 这2个数据
                httpRequest.handleOrder(placeObsr, CURRENT_TABLE, "", ConstantFields.TEMPSTRING, dishes);
                mLoadingDialog = new TimeLoadingDialog.Builder()
                        .setActivity(activity)
                        .setStartContent("操作中...")
                        .setEndContent("下单完成")
                        .setfailContent("下单失败")
                        .setHandler(new SimpleDialogHandler() {
                            @Override
                            public void onConfirm() {
                            }

                            @Override
                            public void onCancel() {
                                httpRequest.stopHandlerAction();
                                placeObsr.onError(new Throwable());
                            }
                        }).build();
                mLoadingDialog.start();
            }

            @Override
            public void onCancel() {
            }
        });
    }

    private Observer<? super Integer> placeObsr() {

        return new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(Integer value) {
                if (value == 200) { // 下单 成功
                    mLoadingDialog.end();
                    placeSuccess();
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
            }
        };
    }

    private void placeSuccess() {
        ConstantFields.cleanCacheData();
        mCartFrag.closeCurrentActivity();
    }


}
